<script setup>
import { computed, ref } from 'vue'
import DiskScheduler from '@/business/DiskScheduler'
import LineChart from '@/components/LineChart.vue'

const currentPosition = ref(30)
const lastPosition = ref(39)
const requestQueue = ref([95, 180, 35, 120, 10, 122, 64, 68])
const inputValue = ref(requestQueue.value.join(', '))
const algorithms = ref([
  { name: '先来先服务(FCFS)', type: 'fcfs' },
  { name: '最短寻道时间优先(SSTF)', type: 'sstf' },
  { name: '电梯算法', type: 'elevator' }
])
const currentAlgorithm = ref('fcfs')

const scheduler = computed(
  () =>
    new DiskScheduler(
      currentPosition.value,
      lastPosition.value,
      requestQueue.value
    )
)

const currentResult = computed(() =>
  scheduler.value.calculate(currentAlgorithm.value)
)

const selectAlgorithm = (type) => {
  currentAlgorithm.value = type
}

const updateRequestQueue = (event) => {
  const newInputValue = event.target.value
  inputValue.value = newInputValue // 更新输入值

  const newArray = newInputValue
    .split(',')
    .map((item) => {
      const num = Number(item.trim())
      return isNaN(num) ? null : num
    })
    .filter((num) => num !== null)

  requestQueue.value = newArray // 总是更新 requestQueue，即使它可能为空
}
</script>

<template>
  <div class="disk-scheduling">
    <div class="input-section">
      <div class="input-group">
        <label>磁头当前位置: </label>
        <input type="number" v-model="currentPosition" />
      </div>
      <div class="input-group">
        <label>上次服务位置: </label>
        <input type="number" v-model="lastPosition" />
      </div>
      <div class="input-group">
        <label>请求队列: </label>
        <input type="text" @input="updateRequestQueue" :value="inputValue" />
      </div>
    </div>

    <div class="algorithm-section">
      <button
        v-for="algo in algorithms"
        :key="algo.name"
        @click="selectAlgorithm(algo.type)"
        :class="{ active: currentAlgorithm === algo.type }"
      >
        {{ algo.name }}
      </button>
    </div>

    <div class="result-section">
      <div class="result-item">
        <h3>移动顺序:</h3>
        <p>{{ currentResult.path.join(' -> ') }}</p>
      </div>
      <div class="result-item">
        <h3>总移动道数:</h3>
        <p>{{ currentResult.totalDistance }}</p>
      </div>
      <div class="result-item">
        <h3>平均移动道数:</h3>
        <p>{{ currentResult.averageDistance.toFixed(2) }}</p>
      </div>
    </div>

    <line-chart :data="currentResult"></line-chart>
  </div>
</template>

<style lang="scss" scoped>
input,
select,
button {
  padding: 8px 12px;
  border: 1px solid #ccc;
  border-radius: 5px;
}

input:focus,
select:focus,
button:focus {
  outline: none;
  border-color: #007bff;
}

.disk-scheduling {
  max-width: 90rem;
  margin: 1rem auto;
  font-family: Arial, sans-serif;

  .input-section {
    margin-bottom: 2rem;
    padding: 2rem;
    background-color: #f5f5f5;
    border-radius: 8px;

    .input-group {
      width: 70%;

      &:not(:first-child) {
        margin-top: 1.5rem;
      }

      &:last-child label {
        margin-left: 2.9rem;
      }
    }

    label {
      font-weight: bold;
      font-size: 1.4rem;
      margin-right: 1rem;
      padding: 0.5rem 0;
    }

    input {
      width: 70%;
    }
  }

  .algorithm-section {
    display: flex;
    gap: 10px;
    margin-bottom: 20px;

    button {
      padding: 10px 20px;
      border: none;
      border-radius: 4px;
      background-color: #e0e0e0;
      cursor: pointer;
      transition: all 0.3s;

      &.active {
        background-color: #007bff;
        color: white;
      }

      &:hover {
        background-color: #007bff;
        color: white;
      }
    }
  }

  .result-section {
    margin-bottom: 20px;
    padding: 15px;
    background-color: #f5f5f5;
    border-radius: 8px;

    .result-item {
      &:not(:last-child) {
        margin: 2rem 0;
      }

      h3 {
        margin: 0;
        font-size: 1.6rem;
        color: #333;
      }

      p {
        margin: 1rem 0;
        color: #666;
        font-size: 1.4rem;
      }
    }
  }

  .chart-container {
    width: 100%;
    height: 400px;
    margin-top: 20px;
  }
}
</style>
